import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/request")
public class httpServletRequest extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        //将响应设置为文本和html格式，并设置utf-8类型
        resp.setContentType("text/html;charset=utf-8");
        // 是一个数据结构
        StringBuffer buffer = new StringBuffer();
        buffer.append(req.getProtocol());
        buffer.append("<br>");
        buffer.append(req.getMethod());
        buffer.append("<br>");
        buffer.append(req.getRequestURI());
        buffer.append("<br>");
        buffer.append(req.getContextPath());
        buffer.append("<br>");
        buffer.append(req.getQueryString());
        buffer.append("<br>");

        Enumeration<String> headerNames = req.getHeaderNames();
        while(headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            buffer.append(headerName + " ");
            buffer.append(req.getHeader(headerName));
            buffer.append("<br>");
        }
        resp.getWriter().write(buffer.toString());
    }
}
